var puc = {
    allowSave: function(isAllowed) {
        if(window.parent.mantle_initialized !== undefined && window.parent.mantle_initialized && 
            window.parent.enableAdhocSave ) {
            if (window.ALLOW_PUC_SAVE === undefined || ALLOW_PUC_SAVE) {
                window.parent.enableAdhocSave(isAllowed);
            }
        }
    },

    refresh_repo: function() {
        if(window.parent.mantle_initialized !== undefined && window.parent.mantle_initialized) {
            window.parent.mantle_refreshRepository();
        }
    },

    save_to_solution: function(filename, solution, path, type, overwrite) {
    	var db = Settings.datasource;
		var shop = Settings.shop;
		var line = $("#lines").val() || 0;
		var from = $("#from").val();
		from = from.length <= 0 ? 0 : new Date(from).getTime();
		var to = $("#to").val();
		to = to.length <= 0 ? 0 : new Date(to).getTime();
    	var repositoryService = new RepositoryService({},{datasource:db,shop:shop,queryName:filename});
    	repositoryService.saveSolution({solution:solution,path:path,action:filename,trajectory:line.toString(),from:from,to:to},function(){
    		puc.refresh_repo();
    	});
    }
};

/**
 * Objects required for BI server integration
 */
var RepositoryBrowserControllerProxy = function() {
    this.remoteSave = puc.save_to_solution;
};

var Wiz = function() {
    this.currPgNum = 0;
};

var WaqrProxy = function() {
    this.wiz = new Wiz();
    this.repositoryBrowserController = new RepositoryBrowserControllerProxy();
};

var gCtrlr = new WaqrProxy();

var savePg0 = function() {};

var initPlugin = function(){
	if(Settings.MODE == "view"){
		$("#controller").css("display","none");
		$('body').mask("Loading");
		var repositoryService = new RepositoryService({},{datasource:"datasource",shop:"shop",queryName:Settings.ACTION});
		repositoryService.loadSolution({solution:Settings.SOLUTION , path:Settings.PATH || "/", action:Settings.ACTION},function(model , response){
			$('body').unmask();
			_.extend(Settings,response); 
			if(Settings.from > 0){
				//$("#from").val(Settings.from);
				$("#from").datetimepicker("setDate" , new Date(Settings.from));
			}
			if(Settings.to > 0){
				$("#to").datetimepicker("setDate" , new Date(Settings.to));
			}
			$("#resource_selector").dialog('close');
			$("#load").trigger("click");
		})
	}else if(Settings.MODE == "edit"){
		//$("#controller").css("display","none");
		$('body').mask("Loading");
		var repositoryService = new RepositoryService({},{datasource:"datasource",shop:"shop",queryName:Settings.ACTION});
		repositoryService.loadSolution({solution:Settings.SOLUTION , path:Settings.PATH || "/", action:Settings.ACTION},function(model , response){
			$('body').unmask();
			_.extend(Settings,response); 
			if(Settings.from > 0){
				//$("#from").val(Settings.from);
				$("#from").datetimepicker("setDate" , new Date(Settings.from));
			}
			if(Settings.to > 0){
				$("#to").datetimepicker("setDate" , new Date(Settings.to));
			}
			$("#resource_selector").dialog('close');
		})
	}
};
